package handler

import (
	"biz-lister/models"
	"github.com/gin-gonic/gin"
)

func Add(c *gin.Context) {
	title := c.PostForm("title")
	content := c.PostForm("content")
	author := c.PostForm("author")
	data := models.Article{
		Title:   title,
		Content: content,
		Author:  author,
	}
	if title == "" || content == "" || author == "" {
		c.JSON(500, gin.H{
			"code": 200,
			"msg":  "内容不能为空",
		})
		return
	}
	err := models.DB.Create(&data).Debug().Error
	if err != nil {
		c.JSON(500, gin.H{
			"code": 200,
			"msg":  "发布失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "发布成功",
	})
	return

}
func List(c *gin.Context) {
	var article []models.Article
	models.DB.Find(&article)
	c.JSON(200, gin.H{
		"code": 200,
		"data": article,
		"msg":  "success",
	})
	return
}
